package com.shiyuanshenghong.web.controller.userlogincontroller;

import com.shiyuanshenghong.entity.UserModel;
import com.shiyuanshenghong.service.userloginservice.UserLoginService;
import com.shiyuanshenghong.util.IsActivate;
import com.shiyuanshenghong.util.MDFiveDigest;
import com.shiyuanshenghong.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by LZQ on 2017/12/29.
 */

@RestController
@RequestMapping("/poverty")
public class UserLoginController {

    @Autowired
    private UserLoginService userLoginService;

    private MDFiveDigest mdFiveDigest;

    @PostMapping("/login")
    public String login(@RequestParam String userEmail,@RequestParam String userPassword){
        UserModel userModelNew=new UserModel();
        userModelNew.setUserEmail(userEmail);
        userModelNew.setUserPassword(userPassword);
        UserModel userModel=userLoginService.queryByUserEmail(userEmail);
        if(userModel==null){
            return "该用户不存在";
        }else if((new Date()).getTime()-userModel.geteEmailActivateTime().getTime()<=Time.MILLISECOND.getText()){
            if(IsActivate.NOTACTIVATED.equals(userModel.getIsActivate())){
                return "该账户还未激活，请去邮箱激活";
            }else{
                return "登录成功";
            }
        }else if(userModel.getUserPassword().equals(mdFiveDigest.md5Hex(userModelNew))){
            return "登录成功";
        }else{
            return "账号或者密码错误";
        }
    }

}
